package com.guodi.map.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guodi.common.constant.StatusCode;
import com.guodi.common.utils.UuidUtil;
import com.guodi.core.boot.ctrl.BaseController;
import com.guodi.core.tool.api.R;
import com.guodi.map.entity.MapFunctionAuth;
import com.guodi.map.service.IMapFunctionAuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @描述：功能参数授权控制类
 * @作者: 代鹏刚
 * @日期: 2020-12-31
 */
@RestController
@RequestMapping("/MapFunctionAuth")
@Api(value = "API - MapFunctionAuthController", tags = "功能参数授权")
public class MapFunctionAuthController extends BaseController {

    @Autowired
    public IMapFunctionAuthService mapFunctionAuthService;

    /**
    * @描述: 根据实体属性分页查询
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2020-12-31
    **/
    @PostMapping("/listByEntity")
    @ApiOperation(value = " 根据实体属性分页查询")
    public R listByEntity(Page page, MapFunctionAuth mapFunctionAuth){
        LambdaQueryWrapper<MapFunctionAuth> wrapper = new QueryWrapper().lambda();
        IPage<MapFunctionAuth> iPage = mapFunctionAuthService.page(page, wrapper);
        return R.data(iPage);
    }

    /**
     * @描述: 根据主键查询
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2020-12-31
     **/
    @PostMapping("/findById")
    @ApiOperation(value = "根据主键查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true,
                    dataType = "string", paramType = "query")
    })
    public R findById(String id) {
        if(StringUtils.isBlank(id)){
            return R.fail(StatusCode.ERROR_CODE_40005,"id不能为空");
        }
        MapFunctionAuth mapFunctionAuth = mapFunctionAuthService.getById(id);
        return R.data(mapFunctionAuth);
    }

    /**
     * @描述: 授权
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2020-12-31
     **/
    @PostMapping("/save")
    @ApiOperation(value = "授权")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "functionId", value = "功能参数ID", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "functionDirIds", value = "功能参数数据ID", allowMultiple = true, dataType = "string", paramType = "query")
    })
    public R save(String roleId, String functionId, String[] functionDirIds){
        //授权前，删除先前的数据
        mapFunctionAuthService.removeByMaps(roleId,functionId);
        if(null != functionDirIds && functionDirIds.length > 0){
            List<MapFunctionAuth> list = new ArrayList<>();
            for (String id:functionDirIds) {
                MapFunctionAuth mapFunctionAuth = new MapFunctionAuth();
                mapFunctionAuth.setId(UuidUtil.get32UUID());
                mapFunctionAuth.setRoleId(roleId);
                mapFunctionAuth.setFunctionDirId(id);
                mapFunctionAuth.setFunctionId(functionId);
                prepareData(mapFunctionAuth, true);
                list.add(mapFunctionAuth);
            }
            mapFunctionAuthService.saveBatch(list);
        }
        return R.status(true);
    }

    /**
     * @描述: 修改
     * @入参:
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2020-12-31
     **/
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public R update(MapFunctionAuth mapFunctionAuth) {
        prepareData(mapFunctionAuth, false);
        mapFunctionAuthService.updateById(mapFunctionAuth);
        return R.data(mapFunctionAuth);
    }

    /**
     * @描述: 根据主键删除
     * @入参: id
     * @出参:
     * @作者: 代鹏刚
     * @日期: 2020-12-31
     **/
    @PostMapping("/delete")
    @ApiOperation(value = "根据主键删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true,
                    dataType = "string", paramType = "query")
    })
    public R delete(String id){
        mapFunctionAuthService.removeById(id);
        return R.status(true);
    }

    /**
    * @描述: 根据roleId获取已授权功能参数
    * @入参:
    * @出参:
    * @作者: 代鹏刚
    * @日期: 2020/12/31 17:09
    **/
    @PostMapping("/getFunctionByRoleIds")
    @ApiOperation(value = "根据roleId获取已授权功能参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleIds", value = "roleId", allowMultiple = true, required = true, dataType = "string", paramType = "query")
    })
    public R getFunctionByRoleIds(String[] roleIds){
        List<MapFunctionAuth> list = mapFunctionAuthService.getFunctionByRoleIds(roleIds);
        return R.data(list);
    }

    private void prepareData(MapFunctionAuth mapFunctionAuth, boolean isSave) {
        if(isSave){
            mapFunctionAuth.setCreator(getUser().getUserId().toString());
            mapFunctionAuth.setCreateTime(new Date());
            //防止误修改
            mapFunctionAuth.setModifier(null);
            mapFunctionAuth.setModifyTime(null);
        }else{
            mapFunctionAuth.setModifier(getUser().getUserId().toString());
            mapFunctionAuth.setModifyTime(new Date());
            //防止误修改
            mapFunctionAuth.setCreator(null);
            mapFunctionAuth.setCreateTime(null);
        }
    }
}
